﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace THO7AlgoritmTimerApplication.Filters
{
    public class InverseFilter : Filter
    {
        public InverseFilter() : base()
        {
        }

        public override Bitmap RunFilter()
        {
            Bitmap output = new Bitmap(_width, _height);
            BitmapData outputData = output.LockBits(new Rectangle(0, 0, _width, _height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            int totalWidth = _width * 3;
            int srcOffset = _sourceData.Stride - totalWidth;
            int optOffset = outputData.Stride - totalWidth;

            unsafe
            {
                byte* srcPnt = (byte*)_sourceData.Scan0;
                byte* optPnt = (byte*)outputData.Scan0;

                for (int y = 0; y < _height; y++)
                {
                    for (int x = 0; x < _width; x++)
                    {
                        optPnt[0] = (byte)(255 - srcPnt[0]);
                        optPnt[1] = (byte)(255 - srcPnt[1]);
                        optPnt[2] = (byte)(255 - srcPnt[2]);
                        srcPnt += 3;
                        optPnt += 3;
                    }

                    srcPnt += srcOffset;
                    optPnt += optOffset;
                }
            }
            output.UnlockBits(outputData);
            return output;
        }
    }
}
